package com.example.pc.myapplication_yjj.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static final String TAG = "DateUtils";

    public static final String FORMAT_0 = "yyyy年M月d日";
    public static final String FORMAT_1 = "M月d日";
    public static final String FORMAT_2 = "hh:mm";
    public static final String FORMAT_3 = "E";


    public static String getWeChatTime(long time) {
        try {
            Log.d(TAG, "getWeChatTime: time:" + time);
            String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.CHINA).format(new Date());
            //今天00:00:00 距离格林威治的的毫秒值
            long todayZore = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA).parse(format).getTime();
            if (time >= todayZore) {
                return long2String(time, FORMAT_2);
            }
            if (time < todayZore && time >= todayZore - 24L * 60 * 60 * 1000) {
                return "昨天";
            }
            if (time < todayZore - 24L * 60 * 60 * 1000 && time >= todayZore - 2 * 24L * 60 * 60 * 1000) {
                return long2String(time, FORMAT_3);
            }
            //今年 yyyy年1月1日 00:00:00 距离格林威治的的毫秒值
            long thisYearZore = new SimpleDateFormat("yyyy", Locale.CHINA).parse(format).getTime();
            if (time >= thisYearZore) {
                return long2String(time, FORMAT_1);
            }
            return long2String(time, FORMAT_0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String long2String(long time, String formatType) {
        return new SimpleDateFormat(formatType, Locale.CHINA).format(new Date(time));
    }


}
